package com.hxd.mall.module.product.home

import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.widget.ImageView
import android.widget.TextView
import androidx.core.graphics.drawable.toBitmap
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.RequestOptions.bitmapTransform
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hxd.mall.R
import com.hxd.mall.base.BaseInjectFragment
import com.hxd.mall.module.product.home.bean.Resource
import com.hxd.mall.utils.open.PictureUtil.isGif
import com.hxd.mall.utils.open.load
import com.hxd.mall.utils.open.loadCircle
import com.hxd.mall.utils.open.loadGif
import com.hxd.mall.utils.open.loadRectangularImg
import com.hxd.mall.widget.CenterCropRoundCornerTransform
import com.shizhefei.view.largeimage.LargeImageView
import org.jetbrains.anko.dip
import org.jetbrains.anko.displayMetrics

@SuppressLint("WrongConstant")
class HomeViewPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
    private var mFragmentList: ArrayList<BaseInjectFragment<*>> = ArrayList()

    fun setFragmentList(fragmentList: ArrayList<BaseInjectFragment<*>>) {
        mFragmentList.clear()
        mFragmentList.addAll(fragmentList)
    }

    override fun getItem(p0: Int) = mFragmentList[p0]

    override fun getCount() = mFragmentList.size
}

class ActiveListAdapter :
    BaseQuickAdapter<Resource, BaseViewHolder>(R.layout.adapter_active_item_layout) {
    override fun convert(helper: BaseViewHolder?, item: Resource?) {
        /* var layoutParams = helper?.itemView?.layoutParams
         layoutParams?.height=layoutParams?.width
         helper?.itemView?.layoutParams=layoutParams*/
        item?.pictUri?.let {
            helper!!.getView<ImageView>(R.id.activePic).loadCircle(it, R.drawable.gray_circle, R.drawable.gray_circle,isFormHome = true)
        }
        helper?.getView<TextView>(R.id.activeName)?.text = item?.title
    }
}

//猜你喜欢列表条目
class LimitListAdapter :
    BaseQuickAdapter<Resource, BaseViewHolder>(R.layout.adapter_limit_item_layout) {
//    BaseQuickAdapter<Resource, BaseViewHolder>(R.layout.adapter_product_detail_item_layout) {
    override fun convert(helper: BaseViewHolder?, item: Resource?) {

        helper?.run {
            val iv = getView<ImageView>(R.id.item_limit_iv)
            (item?.pictUri ?: "").run {
                if (isGif(this))
                    iv.loadGif(this)
                else{
                    iv.load(this)
                }
            }
//        val iv = helper!!.getView<ImageView>(R.id.item_limit_iv)
//            var params = iv.layoutParams
//            val ivHeight = ((iv.context.displayMetrics.widthPixels - iv.context.dip(30)) / 2) * 14 / 17
//            if (ivHeight != -1) {
//                params?.height = ivHeight
//            }
//            iv?.layoutParams = params
//
//            val requestManager = Glide.with(mContext)
//            iv?.setTag(iv.id, requestManager)
//            //设置圆角
//            val placeholder = bitmapTransform(CenterCropRoundCornerTransform(5))
////            requestManager.load(it).apply(placeholder.placeholder(R.mipmap.product_item_icon).error(R.mipmap.product_item_icon)).into(iv)
//            val requestBuilder = requestManager.load(item?.pictUri ?: "").apply(placeholder.placeholder(R.mipmap.product_item_icon).error(R.mipmap.product_item_icon))
//
//            iv.loadRectangularImg(
//                item?.pictUri, R.mipmap.active_card_bg_icon, R.mipmap.active_card_bg_icon, iv = iv, corner = 2,
//                ivHeight = ((iv.context.displayMetrics.widthPixels - iv.context.dip(30)) / 2) * 14 / 17
//            )
//
//            requestBuilder.into(object : SimpleTarget<Drawable>() {
//                override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
//                    val bitmap = resource.toBitmap()
//                    val params = itemView.layoutParams
//
//                    val height = (bitmap.height * mContext.displayMetrics.widthPixels) / bitmap.width
//                    params.height = height
//                    itemView.layoutParams = params
//                    iv.setImage(resource)
//                }
//            })
        }
    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)

        //每次复用时先断开之前的请求
        val imageView = holder.getView<ImageView>(R.id.productPic) ?: return
        val requestManager = imageView.getTag(imageView.id) as RequestManager

        imageView.setImageDrawable(null)

        requestManager.clear(imageView)
    }

}